using UnityEngine;

namespace NL.Advanced
{
    public interface ICampOwner
    {
        CampType Camp { get; }
    }

    //阵容枚举值..0~8  静态值... 101~  相对关系值
    public enum CampType
    {
        None = 0,
        Team1 = 1,
        Team2 = 2,
        Team3 = 3,
        Team4 = 4,
        Team5 = 5,
        Team6 = 6,
        Team7 = 7,
        Team8 = 8,

        Self = 100,    // 仅自己
        Other = 101,   // 除了自己 
        Friend = 111,  // 友军
        Enemy = 112,    // 对对
        All = 110,      // 所有
    }

    //HACK:wcp，临时处理，后面需要优化阵营配置所在的程序集
    public static class Camp
    {
        //阵营关系... >0  友方
        //阵营关系... <0 敌方
        //阵营关系... 0  无关系
        public static short[,] _CAMP_MATRIX = new short[9, 9]
        {
            //              0   1   2   3   4   5   6   7   8  
            /*none */   {   0,  0,  0,  0,  0,  0,  0,  0,  0},
            /*Team1*/   {   0,  0, -1, -1, -1, -1, -1, -1, -1},
            /*Team2*/   {   0, -1,  0,  0,  0,  0,  0,  0,  0},
            /*Team3*/   {   0,  0,  0,  0, -1,  0,  0,  0,  0},
            /*Team4*/   {   0, -1,  0,  0,  0,  0,  0,  0,  0},
            /*Team5*/   {   0,  0,  0,  0,  0,  0,  0,  0,  0},
            /*Team6*/   {   0,  0,  0,  0,  0,  0,  0,  0,  0},
            /*Team7*/   {   0,  0,  0,  0,  0,  0,  0,  0,  0},
            /*Team8*/   {   0, -1,  0,  0,  0,  0,  0,  0,  0},
        };

        public static bool _CampIsEnemy(CampType A, CampType B)
        {
            if (A > CampType.Team8 || B > CampType.Team8) return false;

            return _CAMP_MATRIX[(int)A, (int)B] < 0;
        }

        public static Color[] COLOR_CAMPS = new Color[5]
        {
            Color.white,  Color.red,Color.blue,Color.green,Color.magenta,
        };
        public static Color CampToColor(CampType camp)
        {
            Color color = Color.white;
            if (COLOR_CAMPS != null && COLOR_CAMPS.Length > (int)camp)
            {
                color = COLOR_CAMPS[(int)camp];
            }
            return color;
        }

        public static Color TOColor(this CampType who)
        {
            return CampToColor(who);
        }

        public static CampType _CAMP_SELF;
        public static void SetSelfCamp(CampType camp)
        {
            _CAMP_SELF = camp;
        }



        public static bool ISFriend(ICombatOwner who)
        {
            return who == null ? false : _CampIsFriend(who.Camp);
        }
        public static bool ISFriend(ICombatOwner A, ICombatOwner B)
        {
            return A == null || B == null ? false : _CampIsFriend(A.Camp, B.Camp);
        }
        static bool _CampIsFriend(CampType B)
        {
            return _CampIsFriend(_CAMP_SELF, B);
        }
        static bool _CampIsFriend(CampType A, CampType B)
        {
            if (A > CampType.Team8 || B > CampType.Team8) return false;
            if (A == B) return true;

            return _CAMP_MATRIX[(int)A, (int)B] > 0;
        }

        public static bool ISEnemy(ICombatOwner who)
        {
            return who == null ? false : _CampIsEnemy(_CAMP_SELF, who.Camp);
        }
        public static bool ISEnemy(ICombatOwner A, ICombatOwner B)
        {
            return A == null || B == null ? false : _CampIsEnemy(A.Camp, B.Camp);
        }



        public static bool ISCampOf(CampType _relation, ICombatOwner owner, ICombatOwner target)
        {
            switch (_relation)
            {
                case CampType.Self: return owner == target;
                case CampType.Other: return owner != target;
                case CampType.All: return true;

                default: break;
            }


            CampType A = owner == null ? CampType.None : owner.Camp;
            CampType B = target == null ? CampType.None : target.Camp;

            return _CampCheck(_relation, A, B);
        }
        public static bool _CampCheck(CampType _relation, CampType owner, CampType target)
        {
            switch (_relation)
            {
                case CampType.Self: return owner == target;
                case CampType.Other: return owner != target;
                case CampType.All: return true;
                case CampType.Friend: return _CampIsFriend(owner, target);
                case CampType.Enemy: return _CampIsEnemy(owner, target);
                default: return target == _relation;
            }
        }
    }
}